﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using XnaCanvas;
using matrix = Microsoft.Xna.Framework.Matrix;

namespace Test.Scenes {
    /// <summary>
    /// 画九宫格平面
    /// </summary>
    public partial class XnaPanel2 : UserControl {
        public XnaPanel2() {
            InitializeComponent();
            this.Loaded += XnaPanel_Loaded;
            //this.xcanvas.Trigger.FrameRefresh += Trigger_FrameRefresh;
        }

        void Trigger_FrameRefresh(XTrigger sender) {
            //在这里 写 xo的 旋转
            xo.World *= matrix.CreateRotationX(.01f);
            //xo.World *= matrix.CreateRotationY(.01f);
            //xo.World *= matrix.CreateRotationZ(.01f);
        }
        XObj3 xo;
        void XnaPanel_Loaded(object sender, RoutedEventArgs e) {
            var xs = this.xcanvas.Children[xcanvas.Trigger.Csn];
            //xcanvas.Trigger.Rs.FillMode = Microsoft.Xna.Framework.Graphics.FillMode.WireFrame;
            //xcanvas.Trigger.Rs.CullMode = Microsoft.Xna.Framework.Graphics.CullMode.None;
            xo = new XObj3();
            xs.Add("aaa",xo);
            xo.SetColor(0, 0, 225, 200);
            //xo.effect.Alpha = 1.0f;
            //xo.effect.AmbientLightColor = new Vector3(100, 100, 100);
            var ran = new Random();
            var len = 10;
            Func<float, float, float> func = (x, y) => {
                var r = Math.Pow(x, 2) + Math.Pow(y, 2);
                //var r =  Math.Sin(MathHelper.TwoPi*a/len);
               return (float)r;
                 //return 0;
            };
            xo.ClearBuffer();
            var cellx = Vector3.Right * .1f;
            var celly = Vector3.Up * .1f;
            var lenmax = len * len;
            float offset =len * .1f / 2;
            for (int i = 0; i < len; i++) {
                var xdir = cellx * i;
                for (int j = 0; j < len; j++) {
                    var ydir = xdir + celly * j ;
                    var zposition = func.Invoke(ydir.X,ydir.Y);
                    var zdir = Vector3.Backward * zposition;
                    var transform = matrix.CreateTranslation(zdir);
                    var currentnomal = Vector3.TransformNormal(ydir, transform);
                    //var currentposition = y + Vector3.Backward * zposition;
                    var currentposition = Vector3.Transform(ydir, transform);
                    xo.AddVertex(currentposition, currentnomal);
                    //xo.AddVertex(currentposition, Vector3.Backward);
                    var x3 = i * len + j;
                    var x4 = (i + 1) * len + j;
                    if (i < len - 1 && j < len - 1) {
                        xo.AddIndex(x3);
                        xo.AddIndex(x3 + 1);
                        xo.AddIndex(x4);
                        //
                        xo.AddIndex(x4);
                        xo.AddIndex(x4 + 1);
                        xo.AddIndex(x3 + 1);
                    }
                }
            }
            xo.World = matrix.CreateTranslation(new Vector3(-offset, -offset, 0));
            xo.SetData();
            this.xcanvas.Begin();
        }
    }
}
